#pragma once
#include <vector>
#include "define.h"

#pragma pack(push, 4)
namespace Inverted_File_Index_2_1_0
{
#ifdef _WIN32
	static const UInt64 PAGESIZE = (UInt64)16*1024*1024;
#else
	static const UInt64 PAGESIZE = (UInt64)32*1024*1024;
#endif
	class MemoryManager
	{
		struct PAGE_INFO
		{
			UInt64		size;
			UInt64		used;
			char*		mem;

			PAGE_INFO(const UInt64 _size, char* _mem)
				: size(_size), mem(_mem)
			{
				used = 0;
			}
		};
	private:
		const UInt64			MaxBufferSize;
		UInt64					sizeAllocated;
		std::vector<PAGE_INFO>  pagelist;

		PAGE_INFO* AllocatePage(const UInt64 _pageSize = PAGESIZE);

		PAGE_INFO* GetFreePage(const UInt64 size);
	public:
		MemoryManager(const UInt64 _MaxBufferSize) 
			: MaxBufferSize(_MaxBufferSize>PAGESIZE?_MaxBufferSize/PAGESIZE*PAGESIZE:PAGESIZE), sizeAllocated(0)
		{};

		void releaseMem();

		~MemoryManager()
		{
		  releaseMem();
		};

		void* AllocMem(const UInt64 size);
	};
};
#pragma pack(pop)